package com.aitaoxue.palx.ui.empty;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.util.TDevice;

public class EmptyLayout extends LinearLayout implements OnClickListener {

	public static final int HIDE_LAYOUT = 4;
	public static final int NETWORK_ERROR = 1;
	public static final int NETWORK_LOADING = 2;
	public static final int NODATA = 3;
	public static final int NODATA_ENABLE_CLICK = 5;

	private ProgressBar animProgress;
	private boolean clickEnable = true;
	private Context context;
	private ImageView img;
	private OnClickListener listener;
	private int mErrorState;
	private RelativeLayout mLayout;
	private String strNoDataContent = "";
	private TextView tv;

	public EmptyLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public EmptyLayout(Context context) {
		super(context);
		this.context = context;
		init();
	}

	private void init() {
		View view = View.inflate(context, R.layout.view_error_layout, null);
		img = (ImageView) view.findViewById(R.id.img_error_layout);
		tv = (TextView) view.findViewById(R.id.tv_error_layout);
		mLayout = (RelativeLayout) view.findViewById(R.id.pageerrlayout);
		animProgress = (ProgressBar) view.findViewById(R.id.animProgress);
		setOnClickListener(this);
		img.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (clickEnable) {
					if (listener != null) {
						listener.onClick(v);
					}
				}
			}
		});

		addView(view);
	}

	public void dismiss() {
		mErrorState = HIDE_LAYOUT;
		setVisibility(View.GONE);
	}

	public int getErrorState() {
		return mErrorState;
	}

	public boolean isLoadError() {
		return mErrorState == NETWORK_ERROR;
	}

	public boolean isLoading() {
		return mErrorState == NETWORK_LOADING;
	}

	@Override
	public void onClick(View arg0) {
		if (clickEnable) {
			if (listener != null) {
				listener.onClick(arg0);
			}
		}
	}

	public void setErrorMessage(String msg) {
		tv.setText(msg);
	}

	public void setErrorType(int i) {
		setVisibility(View.VISIBLE);
		switch (i) {
		case NETWORK_ERROR:
			mErrorState = NETWORK_ERROR;
			if (TDevice.hasInternet()) {
				tv.setText(R.string.error_view_load_error_click_to_refresh);
				img.setBackgroundResource(R.drawable.pagefailed_bg);
			} else {
				tv.setText(R.string.error_view_network_error_click_to_refresh);
				img.setBackgroundResource(R.drawable.page_icon_network);
			}
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			clickEnable = true;
			break;
		case NETWORK_LOADING:
			mErrorState = NETWORK_LOADING;
			animProgress.setVisibility(View.VISIBLE);
			img.setVisibility(View.GONE);
			tv.setText(R.string.error_view_loading);
			clickEnable = false;
			break;
		case NODATA:
			mErrorState = NODATA;
			img.setBackgroundResource(R.drawable.page_icon_empty);
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			setTvNoDataContent();
			clickEnable = false;
			break;
		case HIDE_LAYOUT:
			setVisibility(View.GONE);
			break;
		case NODATA_ENABLE_CLICK:
			mErrorState = NODATA_ENABLE_CLICK;
			img.setBackgroundResource(R.drawable.page_icon_empty);
			// img.setBackgroundDrawable(SkinsUtil.getDrawable(context,"page_icon_empty"));
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			setTvNoDataContent();
			clickEnable = true;
			break;
		default:
			break;
		}
	}

	public void setNoDataContent(String noDataContent) {
		strNoDataContent = noDataContent;
	}

	public void setOnLayoutClickListener(View.OnClickListener listener) {
		this.listener = listener;
	}

	public void setTvNoDataContent() {
		if (!strNoDataContent.equals(""))
			tv.setText(strNoDataContent);
		else
			tv.setText(R.string.error_view_no_data);
	}

	@Override
	public void setVisibility(int visibility) {
		if (visibility == View.GONE) {
			mErrorState = HIDE_LAYOUT;
		}
		super.setVisibility(visibility);
	}

}
